package com.lanswon.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.common.util.StringUtil;
import com.lanswon.common.util.TreeNode;
import com.lanswon.entity.ArticleVO;
import com.lanswon.entity.TypeManage;
import com.lanswon.service.TypeManageService;

@Controller
@RequestMapping("admin/typeManage")
public class TypeManageController {
	
	@Autowired
	private TypeManageService typeManageService;
	
	@RequestMapping("index")
	public String index(ModelMap modelMap) {
		
		List<TypeManage> ptypes = typeManageService.getParentTypeList();
		modelMap.addAttribute("ptypes", ptypes);
		
		return "admin/typeManage/index";
	}
	
	@RequestMapping("typeManageList")
	@ResponseBody
	public Map<String, Object> getTypeManageTreeList(){
		Map<String, Object> viewMap = new HashMap<>();
		
		List<TreeNode> treeList = typeManageService.getTypeManageTreeList();
		viewMap.put("data", treeList);
		
		return viewMap;
	}
	
	@RequestMapping("pageList")
	@ResponseBody
	public Map<String, Object> pageList(HttpServletRequest request,HttpServletResponse response,
			Integer page,Integer limit,String name,Long parentId){
		Map<String, Object> viewMap = new HashMap<>();
		
		if(page==null) {
			page = 1;
		}
		if(limit==null) {
			limit = 10;
		}
		Map<String, Object> accessMap = new HashMap<>();
		accessMap.put("currentResult", (page-1)*limit);
		accessMap.put("endResult", limit);
		accessMap.put("name", name);
		accessMap.put("parentId", parentId);
		Integer count = typeManageService.pageListCount(accessMap);
		List<TypeManage> typeList = typeManageService.pageList(accessMap);
		viewMap.put("totalPages", count!=null?count%limit>0?(count/limit)+1:count/limit:0);
		viewMap.put("data", typeList);
		viewMap.put("code", 0);
		viewMap.put("count", count);
		return viewMap;
	}
	
	
	@RequestMapping("typeUpdate")
	public String typeUpdate(ModelMap modelMap,Long id) {
		
		List<TypeManage> ptypes = typeManageService.getParentTypeList();
		modelMap.addAttribute("ptypes", ptypes);
		if(id!=null) {
			TypeManage typeObj = typeManageService.getTypeById(id);
			modelMap.addAttribute("typeObj", typeObj);
		}
		
		
		return "admin/typeManage/typeUpdate";
	}
	
	@RequestMapping("updateType")
	@ResponseBody
	public Map<String, Object> updateType(HttpServletRequest request,HttpServletResponse response, TypeManage tyManage){
		Map<String, Object> viewMap = new HashMap<>();
		try {
			String name = tyManage.getName().trim();
			TypeManage t = typeManageService.getTypeByName(name);
			if(t!=null) {
				viewMap.put("code", 3);
				viewMap.put("msg", "改类型已添加");
				return viewMap;
			}
			if(tyManage.getParentId()==0) {
				tyManage.setParentId(null);
			}
			if(tyManage.getId()!=null) {
				typeManageService.updateType(tyManage);
			}else {
				typeManageService.addType(tyManage);
			}
			viewMap.put("code", 1);
			viewMap.put("msg", "保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			viewMap.put("code", 2);
			viewMap.put("msg", "保存失败");
		}
		return viewMap;
	}
	
	
	@RequestMapping("typeDel")
	@ResponseBody
	public Map<String, Object> typeDel(HttpServletRequest request,HttpServletResponse response,Long id){
		Map<String, Object> viewMap = new HashMap<>();
		try {
			
			if(id!=null) {
				typeManageService.typeDel(id);
			}
			viewMap.put("code", 1);
			viewMap.put("msg", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			viewMap.put("code", 1);
			viewMap.put("msg", "操作失败");
		}
		return viewMap;
	}
	
}
